package com.invoicing.controller;

import com.invoicing.MainController;
import com.invoicing.Service.CommodityService;
import com.invoicing.Service.TypeService;
import com.invoicing.dao.TypeMapper;
import com.invoicing.entity.Commodity;
import com.invoicing.entity.CommodityData;
import com.invoicing.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.util.Callback;
import org.springframework.beans.factory.annotation.Autowired;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import java.awt.*;
import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class UpdateController implements Initializable {

    Commodity commodity = new Commodity();

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private TypeService typeService;


    @FXML
    private TextField id;

    @FXML
    private Button sure;

    @FXML
    private TableView table;

    @FXML
    private TableColumn name;

    @FXML
    private TableColumn type;

    @FXML
    private TableColumn stock;

    @FXML
    private TableColumn warn;

    @FXML
    private TableColumn univalence;

    @FXML
    private TextField alert_name;

    @FXML
    private TextField alert_stock;

    @FXML
    private TextField alert_warn;

    @FXML
    private TextField alert_univalence;

    @FXML
    private RadioButton one;

    @FXML
    private RadioButton two;

    @FXML
    private RadioButton three;

    @FXML
    private RadioButton four;

    @FXML
    private RadioButton five;

    @FXML
    private RadioButton six;



    @FXML
    private void showCommodity(){
        Alert alert_alert = new Alert(Alert.AlertType.WARNING);
        //Commodity commodity = new Commodity();

        name.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("name"));
        type.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("type"));
        stock.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("stock"));
        warn.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("alert"));
        univalence.setCellValueFactory(new PropertyValueFactory<CommodityData, Float>("price"));

        if(id.getText().length()==0){
            alert_alert.setTitle("提示");
            alert_alert.setContentText("请输入商品id！");
            alert_alert.show();
        }
        else{
            if(!commodityService.commodityExitById(Integer.parseInt(id.getText()))){
                alert_alert.setTitle("提示");
                alert_alert.setContentText("该商品不存在！");
                alert_alert.show();
            }
            else{
                commodity = commodityService.findCommodity(Integer.parseInt(id.getText()));
                CommodityData commodityData = new CommodityData(commodity,0,typeService.findById(commodity.getTypeId()).getTypeName(),0,0,0,0);
                ObservableList<CommodityData> data =FXCollections.observableArrayList(commodityData);
                table.setItems(data);
                alert_name.setText(commodity.getName());
                alert_stock.setText(commodity.getStock()+"");
                alert_warn.setText(commodity.getAlert()+"");
                alert_univalence.setText(commodity.getPrice()+"");
                int choose = 0;
                choose = commodity.getTypeId();
                if(choose==1){
                    one.setSelected(true);
                }
                else if(choose==2){
                    two.setSelected(true);
                }
                else if(choose==3){
                    three.setSelected(true);
                }
                else if(choose==4){
                    four.setSelected(true);
                }
                else if(choose==5){
                    five.setSelected(true);
                }
                else {
                    six.setSelected(true);
                }
            }
        }
    }

    @FXML
    private void setAlert(){
        int choose =0;
        Alert alert_alert = new Alert(Alert.AlertType.WARNING);
        Alert alert_success = new Alert(Alert.AlertType.INFORMATION);
        if(one.isSelected()){
            choose=1;
        }
        else if(two.isSelected()){
            choose=2;
        }
        else if(three.isSelected()){
            choose=3;
        }
        else if(four.isSelected()){
            choose=4;
        }
        else if(five.isSelected()){
            choose=5;
        }
        else if(six.isSelected()){
            choose=6;
        }
        else{
            alert_alert.setTitle("提示");
            alert_alert.setContentText("请选择商品类型！");
            alert_alert.show();
        }
        if(alert_name.getText().length()>20){
            alert_alert.setTitle("错误");
            alert_alert.setContentText("商品名长度不可超过20个字符！");
            alert_alert.show();
            alert_name.clear();
        }
        else if(alert_name.getText().length()!=0&&alert_stock.getText().length()!=0&&alert_warn.getText().length()!=0&&alert_univalence.getText().length()!=0){
            commodityService.updateName(commodity.getId(),alert_name.getText());
            commodityService.updateTypeId(commodity.getId(),choose);
            commodityService.updateAlert(commodity.getId(),Integer.parseInt(alert_warn.getText()));
            commodityService.updateStock(commodity.getId(),Integer.parseInt(alert_stock.getText()));
            commodityService.updatePrice(commodity.getId(),Float.parseFloat(alert_univalence.getText()));

            alert_success.setContentText("修改成功");
            alert_success.show();
            table.setItems(null);
            id.clear();

        }
        else{
            alert_alert.setTitle("提示");
            alert_alert.setContentText("请填写完整商品信息！");
            alert_alert.show();
        }

    }
    @FXML
    private void back(){
        MainController.showView(MenuView.class);
    }
    public void initialize(URL location, ResourceBundle resources) {
        final ToggleGroup group = new ToggleGroup();
        one.setToggleGroup(group);
        two.setToggleGroup(group);
        three.setToggleGroup(group);
        four.setToggleGroup(group);
        five.setToggleGroup(group);
        six.setToggleGroup(group);




    }
}
